<template>
  <el-dialog :title="title" :visible.sync="modify" width="60%" append-to-body>
    <el-row>
      <h2>学生信息</h2>
      <el-form ref="form" :model="oldInfor" label-width="110px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="姓名">{{ oldInfor.name }} </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="性别">
              {{ oldInfor.sex ? userSexListChange(oldInfor.sex) : "" }}
            </el-form-item>
          </el-col>

          <el-col :span="8">
            <el-form-item label="注册号">
              {{ oldInfor.registerCode }}
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="照片">
              <img
                :src="oldInfor.avatar"
                alt=""
                style="width: 100px; height: 100px"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="学号">
              {{ oldInfor.sno }}
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="身份证号">
              {{ oldInfor.cardCode }}
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="出生日期">
              {{ oldInfor.birthday }}
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="民族"
              >{{
                oldInfor.nation ? nationTypeListChange(oldInfor.nation) : " "
              }}
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="籍贯">
              {{ oldInfor.nativePlace }}
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="政治面貌">
              {{
                oldInfor.political
                  ? politicalTypeListChange(oldInfor.political)
                  : ""
              }}
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="户籍所在地">
              {{ oldInfor.residence }}
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="所属院系"
              >{{ oldInfor.deptNames }}
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="专业">
              {{ oldInfor.specialityName }}
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="年级">
              {{ oldInfor.gradeName }}
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="班级">
              {{ oldInfor.className }}
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="学生类型">
              {{ oldInfor.studentType }}
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="入学时间">
              {{ oldInfor.joinDate }}
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="毕业时间">
              {{ oldInfor.guaduateDate }}
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="入学前户口类别">
              {{
                oldInfor.nodeType ? nodeTypeListChange(oldInfor.nodeType) : ""
              }}
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="联系电话">
              {{ oldInfor.mobile }}
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="邮箱">
              {{ oldInfor.email }}
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="qq">
              {{ oldInfor.qq }}
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="微信">
              {{ oldInfor.wechatCode }}
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="健康状态">
              {{
                oldInfor.healthStatus
                  ? heablthStatusListChange(oldInfor.healthStatus)
                  : " "
              }}
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="家长电话">
              {{ oldInfor.emergencyMobile }}
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="学籍状态">
              {{
                oldInfor.studentStatus
                  ? studentStatusListChange(oldInfor.studentStatus)
                  : ""
              }}
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="生源地">
              {{ oldInfor.ofStudent }}
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="在校状态">
              {{
                oldInfor.atSchool ? atSchoolListChange(oldInfor.atSchool) : ""
              }}
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="毕业高中">
              {{ oldInfor.highSchool }}
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="家庭地址">
              {{
                oldInfor.addressName
              }}
              <!-- {{
                this.findRegionLabel(addRessOptions,oldInfor.address ) ||
                ""
              }} -->
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-row>
    <h2>获奖信息</h2>
    <el-table :data="oldInfor.studentAwardsList" border style="width: 100%">
      <el-table-column prop="awardName" label="奖项名称"> </el-table-column>
      <el-table-column prop="awardLevel" label="奖项级别">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.basic_awads_level"
            :value="scope.row.awardLevel"
          ></dict-tag>
        </template>
      </el-table-column>
      <el-table-column prop="issuingAuthority" label="颁发机构">
      </el-table-column>
      <el-table-column prop="awardDate" label="获奖日期"> </el-table-column>
    </el-table>
    <h2>家庭信息</h2>
    <el-table :data="oldInfor.studentFamilyList" border style="width: 100%">
      <el-table-column prop="reasonStatus" label="与本人关系" width="100">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.bc_student_famliy"
            :value="scope.row.reasonStatus"
          ></dict-tag>
        </template>
      </el-table-column>
      <el-table-column prop="name" label="姓名" :show-overflow-tooltip="true">
      </el-table-column>
      <el-table-column
        prop="familyMobile"
        label="联系方式"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column
        prop="wechatCode"
        label="微信号"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column
        prop="unit"
        label="工作单位"
        :show-overflow-tooltip="true"
      >
      </el-table-column>

      <el-table-column
        prop="familyAddress"
        label="家庭地址"
        :show-overflow-tooltip="true"
        :formatter="formatRegion"
      >
        
      </el-table-column>
      <el-table-column
        prop="postalCode"
        label="家庭邮编"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column
        prop="familyRemark"
        label="家庭备注"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
    </el-table>
    <h2>宿舍违纪信息</h2>
    <el-table :data="oldInfor.dormViolateList" border style="width: 100%">
      <el-table-column type="index" width="55" label="序号" align="center" />
      <el-table-column label="公寓楼" align="center" prop="houseNo" show-overflow-tooltip/>
      <el-table-column label="楼层" align="center" prop="floorNo" show-overflow-tooltip/>
      <el-table-column label="房间号" align="center" prop="roomNo" show-overflow-tooltip/>
      <el-table-column label="学生姓名" align="center" prop="studentName" show-overflow-tooltip/>
      <el-table-column label="违纪类型" align="center" prop="violateType" show-overflow-tooltip>
        <template slot-scope="scope">
          <dict-tag :options="dict.type.dorm_violate_type" :value="scope.row.violateType" />
        </template>
      </el-table-column>
      <el-table-column label="违纪时间" align="center" prop="violateTime" show-overflow-tooltip/>
      <el-table-column label="违纪事实" align="center" prop="violateContent" show-overflow-tooltip />
      <el-table-column label="处理结果" align="center" prop="violateResult" show-overflow-tooltip />
      <el-table-column label="处理状态" align="center" prop="status" show-overflow-tooltip>
        <template slot-scope="scope">
          <dict-tag :options="[{ label: '已删除', value: '2' }, { label: '未处理', value: '0' }]" :value="scope.row.delFlag" />
        </template>
      </el-table-column>
      <el-table-column label="处理时间" align="center" prop="updateTime"  show-overflow-tooltip/>
    </el-table>
    <h2>图书借阅</h2>
     <el-table :data="oldInfor.books" border style="width: 100%">
      <el-table-column type="index" width="55" label="序号" align="center" />
      <el-table-column label="书名" align="center" prop="title" show-overflow-tooltip/>
      <el-table-column label="图书编号" align="center" prop="barcode" show-overflow-tooltip/>
      <el-table-column label="金额" align="center" prop="price" show-overflow-tooltip>
         <template slot-scope="scope">
         {{ scope.row.price }}元
        </template>
      </el-table-column>
     </el-table>
  </el-dialog>
</template>

<script>
import { studentInfor } from "@/api/information/studentInfor";

import { getInforStuapprinfo } from "@/api/flowable/stuapply.js";
import { getDicts } from "@/api/system/dict/type";
import { addersTree } from "@/api/flowable/stuapply.js";

export default {
  components: {},
  props: {
    title: { type: String },
    deptOptions: {
      type: Array,
      default: function () {
        return [];
      },
    },
    // addRessOptions: {
    //   type: Array,
    //   default: function () {
    //     return [];
    //   },
    // },
    showType: {
      type: String,
      default: "operate",
    },
  },

  dicts: [
    "bc_student_status", // 学籍状态
    "bc_political_type", // 政治面貌
    "bc_nation_type", // 民族
    "bc_student_type", // 学生类型
    "bc_node_type", // 入学前户口类别
    "follow_status", // 是否关注对象
    "sys_user_sex", // 性别
    "bc_student_famliy", // 家庭关系
    "bc_health_status", // 健康状态
    "basic_awads_level", // 获奖级别
    "basic_at_school", // 是否在校
    "dorm_violate_type", // 违纪类型
  ],
  computed: {},
  data() {
    return {
      modify: false,
      newInfor: {},
      oldInfor: {},
      form: {
        reason: "",
        apprStatus: "",
        baiId: "",
      },
      userSexList: [],
      nationTypeList: [],
      politicalTypeList: [],
      heablthStatusList: [],
      studentStatusList: [],
      atSchoolList: [],
      nodeTypeList: [],
      currentLabel: "",
      parentLabel: "",
      addRessOptions:[]
    };
  },

  watch: {},
  computed: {
    // // 过滤出旧数据存在的字段
    filteredFields() {
      return this.formFields.filter((field) => {
        // Vue2 兼容性写法：使用 Object.prototype 判断属性存在性
        return Object.prototype.hasOwnProperty.call(this.newInfor, field.key);
      });
    },
  },
  created() {
    getDicts("sys_user_sex").then((res) => {
      this.userSexList = res.data;
    });
    getDicts("bc_nation_type").then((res) => {
      this.nationTypeList = res.data;
    });
    getDicts("bc_political_type").then((res) => {
      this.politicalTypeList = res.data;
    });
    getDicts("bc_health_status").then((res) => {
      this.heablthStatusList = res.data;
    });
    getDicts("bc_student_status").then((res) => {
      this.studentStatusList = res.data;
    });
    getDicts("basic_at_school").then((res) => {
      this.atSchoolList = res.data;
    });
    getDicts("bc_node_type").then((res) => {
      this.nodeTypeList = res.data;
    });
    addersTree().then((res) => {
      console.log(res, "家庭地址");
      this.addRessOptions = res.data;
    });
  },
  methods: {
    getDisplayValue(key, value) {
      switch (key) {
        case "nodeType":
          return this.nodeTypeList[value].dictLabel || value;
        case "healthStatus":
          return this.heablthStatusList[value].dictLabel || value;
        case "nation":
          return this.nationTypeList[value].dictLabel || value;
        case "studentStatus":
          return this.studentStatusList[value].dictLabel || value;
        case "sex":
          return this.userSexList[value].dictLabel || value;
        case "political":
          return this.politicalTypeList[value].dictLabel || value;
        case "political":
          return this.politicalTypeList[value].dictLabel || value;
        default:
          return value;
      }
    },
    // 性别下拉框
    userSexListChange(val) {
      console.log(val); //value值
      let assetData = this.userSexList.find((item) => item.dictValue === val);
      val = assetData.dictLabel; //label值
      return val;
    },
    // 民族下拉框
    nationTypeListChange(val) {
      console.log(val, "minzu"); //value值
      let assetData = this.nationTypeList.find(
        (item) => item.dictValue === val
      );
      val = assetData.dictLabel; //label值
      return val;
    },
    // 政治面貌下拉框
    politicalTypeListChange(val) {
      console.log(val); //value值
      let assetData = this.politicalTypeList.find(
        (item) => item.dictValue === val
      );
      val = assetData.dictLabel; //label值
      return val;
    },
    // 健康状态下拉框
    heablthStatusListChange(val) {
      console.log(val); //value值
      let assetData = this.heablthStatusList.find(
        (item) => item.dictValue === val
      );
      val = assetData.dictLabel; //label值
      return val;
    },
    // 学籍状态下拉框
    studentStatusListChange(val) {
      console.log(val); //value值
      let assetData = this.studentStatusList.find(
        (item) => item.dictValue === val
      );
      val = assetData.dictLabel; //label值
      return val;
    },
    // 在校状态下拉框
    atSchoolListChange(val) {
      console.log(val); //value值
      let assetData = this.atSchoolList.find((item) => item.dictValue === val);
      val = assetData.dictLabel; //label值
      return val;
    },
    // 入学前户口类别
    nodeTypeListChange(val) {
      console.log(val); //value值
      let assetData = this.nodeTypeList.find((item) => item.dictValue === val);
      val = assetData.dictLabel; //label值
      return val;
    },
    
    // 审核通过
    handlePass() {
      this.$emit("pass", this.form);
    },
    // 审核通过
    application() {
      this.$emit("reject", this.form);
    },
    // 审核通过
    refuse() {
      this.$emit("refuse", this.form);
    },
    // 取消
    closeDia() {
      this.modify = false;
    },
    examineDialog(data) {
      this.modify = true;

      // this.newInfor = this.newInfor.Parameters
      studentInfor(data)
        .then((response) => {
          this.oldInfor = response.data;
          console.log(response, this.oldInfor, "学生信息");
          //  this.oldInfor.address = this.getRegionLabel(this.addRessOptions, this.oldInfor.address);
          
          // this.oldInfor = this.transformObject(this.oldInfor);
        })
        .catch((err) => {
          console.log(err);
        });
    },
// 递归查找目标代码对应的标签（区县名称）
    getRegionLabel(nodes, targetCode) {
      // 边界条件：如果节点数组为空，直接返回
     for (const item of nodes) {
        // 1. 先检查当前节点是否匹配
        if (String(item.unitCode) === targetCode) {
          return item.label; // 找到直接返回
        }
        // 2. 若有子节点，递归搜索子节点
        if (item.children && item.children.length > 0) {
          const childResult = this.getRegionLabel(item.children, targetCode);
          // 子节点找到结果，立即返回
          if (childResult) {
            return childResult;
          }
        }
      }
      // 遍历完所有节点未找到
      return null;
   
  },

     // 格式化函数：将代码转换为省市区文本
    formatRegion(row, column, code) {
      // 递归查找函数：遍历节点数组，匹配目标代码
      const findRegionPath = (nodes, targetCode, path = []) => {
        for (const node of nodes) {
          // 记录当前节点的标签（如“北京市”“北京城区”）
          const currentPath = [...path, node.label];
          // 找到目标代码，返回完整路径
          if (node.unitCode === targetCode) {
            return currentPath;
          }
          // 若有子节点，继续递归查找
          if (node.children && node.children.length) {
            const result = findRegionPath(node.children, targetCode, currentPath);
            if (result) return result;
          }
        }
        return null; // 未找到
      };

      // 从根数组开始查找（遍历所有省级行政区）
      const regionPath = findRegionPath(this.addRessOptions, code);
      return regionPath ? regionPath.join(" - ") : "未知地区";
    },
    findLabelByCode(data, code) {
      for (const item of data) {
        if (item.unitCode === code) {
          return item.label; // 直接匹配成功
        }
        // 若存在子节点，递归查找子层级
        if (item.children && item.children.length > 0) {
          const result = this.findLabelByCode(item.children, code);
          if (result) {
            return result; // 子层级中找到结果则返回
          }
        }
      }
      return null; // 未找到
    },
    getLabelByValue(value, optionList) {
      console.log(value,optionList,'sdfhdsh')
      for (const option of optionList) {
        if (option.unitCode === value) {
          return option.label;
        }
        if (option.children) {
          const result = this.getLabelByValue(value, option.children);
          if (result) {
            return result;
          }
        }
      }
      return "未找到匹配的 label";
    },
    //     getFieldColor (key)  {
    //   return oldFormData.value[key]!== newFormData.value[key]? 'red' : 'inherit';
    // },

    transformObject(obj) {
      return {
        name: obj.name || "",
        sex: obj.sex || "",
        registerCode: obj.registerCode || "",
        sno: obj.sno || "",
        cardCode: obj.cardCode || "",
        birthday: obj.birthday || "",
        nation: obj.nation || "",
        nativePlace: obj.nativePlace || "",
        political: obj.political || "",
        residence: obj.residence || "",
        deptId: obj.deptId ? String(obj.deptId) : "",
        specialityId: obj.specialityId ? String(obj.specialityId) : "",
        avatar: obj.avatar || "",
        gradeId: obj.gradeId ? String(obj.gradeId) : "",
        classId: obj.classId ? String(obj.classId) : "",
        studentType: obj.studentType || "",
        joinDate: obj.joinDate || "",
        guaduateDate: obj.guaduateDate || "",
        nodeType: obj.nodeType || "",
        mobile: obj.mobile || "",
        email: obj.email || "",
        qq: obj.qq || "",
        wechatCode: obj.wechatCode || "",
        healthStatus: obj.healthStatus || "",
        emergencyMobile: obj.emergencyMobile || "",
        studentStatus: obj.studentStatus || "",
        ofStudent: obj.ofStudent || "",
        atSchool: obj.atSchool || "",
        highSchool: obj.highSchool || "",
        address: obj.address || "",
        studentAwardsList: obj.studentAwardsList || [],
        studentFamilyList: obj.studentFamilyList || [],
      };
    },
    transNewObject(obj) {
      return {
        name: obj.applyName || "",
        sex: obj.sex || "",
        registerCode: obj.applyRegisterCode || "",
        sno: obj.sno || "",
        cardCode: obj.cardCode || "",
        birthday: obj.birthday || "",
        nation: obj.nation || "",
        nativePlace: obj.nativePlace || "",
        political: obj.political || "",
        residence: obj.residence || "",
        deptId: obj.deptId ? String(obj.deptId) : "",
        specialityId: obj.specialityId ? String(obj.specialityId) : "",
        avatar: obj.avatar || "",
        gradeId: obj.gradeId ? String(obj.gradeId) : "",
        classId: obj.classId ? String(obj.classId) : "",
        studentType: obj.studentType || "",
        joinDate: obj.joinDate || "",
        guaduateDate: obj.guaduateDate || "",
        nodeType: obj.nodeType || "",
        mobile: obj.mobile || "",
        email: obj.email || "",
        qq: obj.qq || "",
        wechatCode: obj.wechatCode || "",
        healthStatus: obj.healthStatus || "",
        emergencyMobile: obj.emergencyMobile || "",
        studentStatus: obj.studentStatus || "",
        ofStudent: obj.ofStudent || "",
        atSchool: obj.atSchool || "",
        highSchool: obj.highSchool || "",
        address: obj.address || "",
        studentAwardsList: obj.studentAwardsList || [],
        studentFamilyList: obj.studentFamilyList || [],
      };
    },
    compareObjects(oldInfor, newInfor) {
      const result = {};
      const allKeys = new Set([
        ...Object.keys(oldInfor),
        ...Object.keys(newInfor),
      ]);

      allKeys.forEach((key) => {
        const oldValue = oldInfor[key];
        const newValue = newInfor[key];
        let isDifferent = false;

        if (
          typeof oldValue === "object" &&
          oldValue !== null &&
          typeof newValue === "object" &&
          newValue !== null
        ) {
          result[key] = {
            value: this.compareObjects(oldValue, newValue),
            isDifferent: false,
          };
          for (const subKey in result[key].value) {
            if (result[key].value[subKey].isDifferent) {
              result[key].isDifferent = true;
              break;
            }
          }
        } else {
          isDifferent = oldValue !== newValue;
          result[key] = {
            oldValue,
            newValue,
            isDifferent,
          };
        }
      });

      return result;
    },
  },
};
</script>

<style scoped>
</style>